<?php

function C($name = null, $value = null, $append = false)
{
    if ($name === null)
        return \core\kyo::$config;

    if (is_array($name)) {
        \core\kyo::$config = $name + \core\kyo::$config;
    } else if ($value !== null) {
        if (!$append) {
            \core\kyo::$config[$name] = $value;
        } else if (is_array(\core\kyo::$config[$name])) {
            array_push(\core\kyo::$config[$name], $value);
        } else {
            \core\kyo::$config[$name] += $value;
        }

    } else {
        return \core\kyo::$config[$name];
    }
}

function M($table = null)
{
    $model = \core\Model::getInstance($table);
    return $model;
}

function E($mes, $code = 0)
{
    throw new \Exception($mes, $code);
}

function dump_array($array)
{
    static $level = 0;

    $indent = "";

    for ($i = 0; $i < $level; ++$i) {
        $indent .= '&emsp;&emsp;';
    }

    $level++;

    $str = $indent.'array (<br />';
    foreach ($array as $key => $val) {
        if (is_array($val)) {
            $str .= dump_array($val);
            $level--;
        } else {
            $str .= $indent.$indent."'".$key."'&nbsp;=>&nbsp;'".$val."',<br />";
        }
    }
    $str .= $indent.')<br />';

    return $str;
}

function dump($val, $return = true, $title = null)
{
    if (is_array($val))
        $content = dump_array($val);
    else
        $content = $val;

    if ($title === null)
        $title = "";
    else
        $title = '<h1 style="border-bottom:1px #000 solid">'.$title.'</h1>';

    $html = <<< EOF
<div class=".dump" style="border: 1px #f00 solid; font-size:14px;padding:10px;">
    $title
    $content
</div>
EOF;
    if ($return)
        return $html;
    echo $html;
}
